import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/hj_page/home_business/search_page/view/search_hot_table_logic.dart';
import 'package:tiktokadult/hj_page/home_business/search_page/view/search_hot_table_view.dart';
import 'package:tiktokadult/hj_page/home_business/search_page/search_result_page/search_result_main_page.dart';
import 'package:tiktokadult/hj_page/home_business/search_page/widget/search_app_bar.dart';
import 'package:tiktokadult/hj_page/home_business/search_page/widget/search_hot_tag_view.dart';
import 'package:tiktokadult/hj_utils/global.dart';
import 'package:tiktokadult/hj_utils/text_util.dart';

import '../../../hj_model/video_model.dart';
import '../../../hj_utils/api_service/search_service.dart';

abstract class SearchMainLogic extends GetxController {
  PageController pageController = PageController();
  int tagPage = 1;
  SearchHotTagType hotType = SearchHotTagType.video;
  List<String> sortTypeList = [
    '本周最热',
    '本月最热',
    '上月最热',
  ];

  //是否需要跳转到结果页
  onSubmitted(String text, {bool needPush = true}) async {
    if (TextUtil.isEmpty(text)) {
      showToast("请输入搜索内容");
      return;
    }
    searchTextController.text = text;
  }

  List<Widget> recommendChildren();

  @override
  onClose() {
    searchTextController.clear();
    super.onClose();
  }
}

class VideoSearchMainController extends GetxController {

  List<VideoModel>? dataArr;

  @override
  void onInit() {
    super.onInit();
    _loadHotData();
  }


  void _loadHotData() async {
    final res = await SearchService.getSearchHotVideos(1, 12, 1);
    dataArr ??= [];
    dataArr?.addAll(res?.videos ?? []);
    update();
  }

  onSubmitted(String text, {bool needPush = true}) async {
    if (TextUtil.isEmpty(text)) {
      showToast("请输入搜索内容");
      return;
    }
    searchTextController.text = text;
    await Get.to(() => SearchResultMainPage(text));
  }


}

class ACGSearchMainController extends SearchMainLogic {
  @override
  SearchHotTagType get hotType => SearchHotTagType.cartton;
  @override
  onSubmitted(String text, {bool needPush = true}) async {
    super.onSubmitted(text, needPush: needPush);
    await Get.to(() => SearchResultMainPage(text));
  }

  @override
  libraryOnTap() {}

  @override
  List<Widget> recommendChildren() {
    return [
      SearchHotTableView<ACGHotController>(type: 0),
      SearchHotTableView<ACGHotController>(type: 1),
      SearchHotTableView<ACGHotController>(type: 2),
    ];
  }
}

T instanceController<T>() {
  final t = T.toString();
  if (t == 'ACGSearchMainController') return ACGSearchMainController() as T;
  if (t == 'VideoSearchMainController') return VideoSearchMainController() as T;
  throw '$t 没有实现 请继承SearchMainLogic实现';
}
